// pages/register/register.js
const app = getApp();
const db = wx.cloud.database();
const userList = db.collection('user');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    address: '',
    age: 0,
    name: '',
    password: '',
    sex: '',
    department: '',
    hospital: '',
    enroll: true,
    inputSex: "女",
    show: false,
    actions: [
      {
        name: '市级医院',
      },
      {
        name: '县级医院',
      },
      {
        name: '乡镇医院',
        subname: '选择医院',
      },
    ], 
  },
  changeEnrool(e){
    this.setData({
      enroll:false
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  /**
   * 接受前端返回的数据
   */
  onSelect(event) {
    this.hospital = event.detail.name;
    console.log(event);
  },
  onChooseHospital(){
    this.setData({ show: true });
  },
  inputName(event){
    this.name = event.detail.value;
    console.log(this.name);
  },
  inputAge(event){
    this.age = event.detail.value;
  },
  inputAddress(event){
    this.address = event.detail.value;
  },
  inputPhone(event){
    this.account = event.detail.value;
  },
  inputPassword(event){
    this.password = event.detail.value;
  },
  inputDepartment(event){
    this.department = event.detail.value;
  },
  register(){
    let that = this;
    userList.where({
      _openid: app.globalData.openid
     }).get({
      success: function(res) {
       let userInfos = res.data;
       if (userInfos && userInfos.length > 0) {
        let user = userInfos[0];
        if (user && user.name) {
         wx.showModal({
          title: '提示',
          content: '您已注册，确定要更新账号密码吗？',
          success: function(res) {
           if (res.confirm) {
            that.saveuserinfo();
           }
          }
         })
        }
       } else {
        that.saveuserinfo();
       }
      }
     })
  },
  saveuserinfo() {
   //向用户表中存入信息
  userList.doc('_openid').set({
    data: {
     name: this.name,
     sex : this.sex,
     age : this.age,
     address: this.address,
     account: this.account,
     password: this.password,
     isappointment: false,
     status: 1,
    },
   }).then(res => {
     wx.showToast({
       title: '注册成功',
       icon:"success",
       duration:2000,
       //提示2秒后自动跳转到首页
       success:function(){
         setTimeout(function(){
           wx.switchTab({
           url: '/pages/mine/mine',
           })
         },2000)
       }
     })
   })
  },
  application(){
    let that = this;
    userList.where({
      _openid: app.globalData.openid
     }).get({
      success: function(res) {
       let userInfos = res.data;
       if (userInfos && userInfos.length > 0) {
        let user = userInfos[0];
        if (user && user.name) {
         wx.showModal({
          title: '提示',
          content: '已提交申请，请耐心等待！',
          success: function(res) {
           if (res.confirm) {
            that.savedocuserinfo();
           }
          }
         })
        }
       } else {
        that.savedocuserinfo();
       }
      }
     })
  },
  savedocuserinfo() {
   //向用户表中存入信息
  userList.doc('_openid').set({
    data: {
     name: this.name,
     account: this.account,
     password: this.password,
     hospital: this.hospital,
     department: this.department,
     isverify: false,
     isappointed: 0,
     status: 2,
    }
   }).then(res => {
     wx.showToast({
       title: '已提交',
       icon:"success",
       duration:2000,
       //提示2秒后自动跳转到首页
       success:function(){
         setTimeout(function(){
           wx.switchTab({
           url: '/pages/index/index',
           })
         },2000)
       }
     })
   })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  radioChange(e){
    this.inputSex = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})